<?php namespace Admin\Controller; 
//品牌控制器
class BrandController extends CommonController{
    private $Brandmodel;
//  自定义构造函数
    public function __auto(){
        //引Model文件
        $this->Brandmodel = new \Common\Model\Brand;
    } 
    
//  首页
    public function index(){
		//排序
		if(IS_POST){
			
//			p(Q("post."));
			foreach (Q('post.') as $k => $v) {
				$this->Brandmodel->where("bid={$k}")->save(array('sort'=>$v));	
			}
			View::success('修改成功',U('index'));
		}
		
    	$BrandData = $this->Brandmodel->orderBy('sort','ASC')->get();
		if(!$BrandData) View::error('你还没有添加品牌，请先添加品牌',U('add'));
//		p($BrandData);
		View::with("BrandData",$BrandData);
        View::make();
    }
    
//  添加
    public function add(){
//  	p(Q("post."));
		if(IS_POST){
			if(!$this->Brandmodel->store()) View::error($this->Brandmodel->getError());
			View::success("添加成功",U('index'));
		}
        View::make();
    }
//  编辑
    public function edit(){
//  	2.修改
    	if(IS_POST){
    		if(!$this->Brandmodel->edit()) View::error($this->Brandmodel->getError());
				View::success("修改成功",U('index'));
			}
//  	p(Q("post."));
//  	1.获取原数据
    	$bid = Q("get.bid",0,'intval');
		$BrandData = $this->Brandmodel->where("bid={$bid}")->find();
//		p($BrandData);
		View::with('BrandData',$BrandData);
        View::make();
    }
//  删除
    public function del(){
        $bid = Q("get.bid",0,'intval');
		$this->Brandmodel->where("bid={$bid}")->delete();
		View::success("删除成功",U('index'));
    }
}